package branch_and_price;

import gurobi.GRB;
import gurobi.GRBEnv;
import gurobi.GRBException;
import gurobi.GRBLinExpr;
import gurobi.GRBModel;
import gurobi.GRBVar;

import java.util.ArrayList;

public class BranchAndPrice {
    public static void main(String[] args) {

        int m = Dataset.capacity.length;
        int n = Dataset.profit[0].length;
        ArrayList<ArrayList<double[]>> assignments = new ArrayList<ArrayList<double[]>>(m);

        // 生成初始方案
        try {

            GRBEnv env = new GRBEnv(true);
            env.start();
            GRBModel model;
            for (int i = 0; i < m; i++) {
                model = new GRBModel(env);
                model.set("OutputFlag", "0");

                GRBVar[] x = model.addVars(n, GRB.BINARY);

                GRBLinExpr expr = new GRBLinExpr();
                expr.addTerms(Utils.p_i(i), x);
                model.setObjective(expr, GRB.MAXIMIZE);

                expr = new GRBLinExpr();
                expr.addTerms(Utils.w_i(i), x);
                model.addConstr(expr, GRB.LESS_EQUAL, Utils.c(i), "c0");

                model.optimize();

                ArrayList<double[]> temp_assignments = new ArrayList<double[]>();
                double[] s = new double[n];
                for (int j = 0; j < model.getVars().length; j++) {
                    s[j] = model.getVars()[j].get(GRB.DoubleAttr.X);
                }
                temp_assignments.add(s);
                assignments.add(temp_assignments);
            }

        } catch (GRBException e) {
            System.out.println("Error code: " + e.getErrorCode() + ". " + e.getMessage());
        }

        System.out.println("开始求解");
        Solver solver = new Solver(assignments);
        solver.solve();
    }
}
